package sicnu.cs.aps.rest;

import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import sicnu.cs.aps.common.ResultInfo;
import sicnu.cs.aps.common.ResultInfoUtil;
import sicnu.cs.aps.common.util.CollectionUtil;
import sicnu.cs.aps.domain.entity.GroupMemo;
import sicnu.cs.aps.domain.entity.StudentInfo;
import sicnu.cs.aps.domain.form.GroupMemoForm;
import sicnu.cs.aps.domain.form.QueryForm;
import sicnu.cs.aps.domain.vo.GroupMemoVo;
import sicnu.cs.aps.service.IGroupMemoService;
import sicnu.cs.aps.service.IStudentInfoService;
import sicnu.cs.aps.service.impl.StudentInfoServiceImpl;

import javax.validation.Valid;

import java.util.*;
import java.util.function.ToLongFunction;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static sicnu.cs.aps.common.util.RequestUtil.getCurrentUserId;

/**
 * @author CaiKe
 * @date 2022/5/26 15:39
 */
@Slf4j
@RestController
@RequestMapping("/group")
@RequiredArgsConstructor
@Api(tags = {"团队成员备忘录"})
public class GroupMemoResource {

    private final IGroupMemoService groupMemoService;
    private final IStudentInfoService studentInfoService;

//    @GetMapping("/")
//    @ApiOperation("根据Id获取小组成员备忘录内容")
//    public ResultInfo<GroupMemo> getGroupMemoById(@RequestParam Long id) {
//        return ResultInfoUtil.buildSuccess(groupMemoService.getById(id));
//    }

    @PostMapping("/")
    @ApiOperation("新增小组成员备忘录")
    public ResultInfo<Void> addGroupMemo(@Valid GroupMemoForm form) {
        var groupToSave = GroupMemo.builder()
                .userId(getCurrentUserId())
                .members(CollectionUtil.combineList(form.getMemberIds()))
                .remark(form.getRemark())
                .build();
        groupMemoService.save(groupToSave);
        return ResultInfoUtil.buildSuccess();
    }

//    @PutMapping("/")
//    @ApiOperation("修改小组成员备忘录")
//    public ResultInfo<Void> updateGroupMemo(@Valid GroupMemoForm form) {
//        var groupToUpdate = GroupMemo.builder()
//                .id(form.getId())
//                .userId(getCurrentUserId())
//                .members(CollectionUtil.combineList(form.getMemberIds()))
//                .remark(form.getRemark())
//                .build();
//        groupMemoService.updateById(groupToUpdate);
//        return ResultInfoUtil.buildSuccess();
//    }

//    @DeleteMapping("/")
//    @ApiOperation("修改小组成员备忘录（批量）")
//    public ResultInfo<Void> deleteGroupMemo(@RequestBody List<Long> ids) {
//        groupMemoService.removeByIds(ids);
//        return ResultInfoUtil.buildSuccess();
//    }

    @GetMapping("/list")
    @ApiOperation("查询当前用户的小组成员备忘录")
    public ResultInfo<List<GroupMemoVo>> listGroupMemo() {
        var memos = groupMemoService.listGroupMemos(getCurrentUserId());
        if (memos.isEmpty()) {
            return ResultInfoUtil.buildSuccess();
        }
        final Set<Long> ids = memos
                .stream()
                .flatMapToLong(
                        s -> CollectionUtil.separateStringExcludeEmpty(s.getMembers())
                                .stream()
                                .mapToLong(Long::parseLong))
                .boxed()
                .collect(Collectors.toSet());
        if (ids.isEmpty()) {
            return ResultInfoUtil.buildSuccess();
        }
        // 获取学生Map
        final Map<Long, StudentInfo> students = studentInfoService.listByIds(ids)
                .stream()
                .collect(Collectors.toMap(StudentInfo::getUserId, stu -> stu));
        // 对应封装+转型
        final List<GroupMemoVo> res = memos.stream().map(memo -> GroupMemoVo.builder()
                .id(memo.getId())
                .remark(memo.getRemark())
                .members(
                        CollectionUtil.separateStringExcludeEmpty(memo.getMembers())
                                .stream()
                                .map(Long::parseLong)
                                .map(students::get)
                                .collect(Collectors.toList()))
                .build()).collect(Collectors.toList());
        return ResultInfoUtil.buildSuccess(res);
    }

}